<?php
/*
Plugin Name: admin page Helper
Description: Some function to help developer to write admin page e panel   
Plugin URI:  http://www.decristofano.it/
Version:     0.3
Author:      Luca De Cristofano
Author URI:  http://www.decristofano.it/
*/


function admin_css() {
	$url = plugins_url( 'admin_helper.css' , __FILE__ );
	@wp_register_style('admin_helper', $url);
	@wp_enqueue_style('admin_helper');
	// carica qui anche i js
}

function admin_menu($parent, $page_title, $menu_title, $function_name, $menu_slug, $position='', $capability='edit_plugins', $icon_url='') {
	// add a panel in wordpres menu
	
	
	switch ($parent) {
		case '' : $parent = ''; break;
		case 'Dashboard': $parent='index.php'; break;
		case 'Posts': $parent='edit.php'; break;
		case 'Media': $parent='upload.php'; break;
		case 'Links': $parent='link-manager.php'; break;
		case 'Pages': $parent='edit.php?post_type=page'; break;
		case 'Comments': $parent='edit-comments.php'; break;
		case 'Appearance': $parent='themes.php'; break;
		case 'Plugins': $parent='plugins.php'; break;
		case 'Users': $parent='users.php'; break;
		case 'Tools': $parent='tools.php'; break;
		case 'Settings': $parent='options-general.php'; break;
		default :	$parent='options-general.php'; break;
	}
	
	//add_options_page($page_title, $menu_title, $capability, $menu_slug, $function);
	//add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function_name, $icon_url, $position );
	
	//$menu_slug= strtolower(str_replace(array(' ','@', 'ˆ','�', '˜', 'Ž', '�', '"', '¡'), array('_','_','a','e','o','e','u','i','_'), $menu_title)).'___x';
	if ($parent=='') 
		add_menu_page   (          $page_title, $menu_title, $capability, $menu_slug, $function_name, $icon_url, $position );
	else
		add_submenu_page( $parent, $page_title, $menu_title, $capability, $menu_slug, $function_name ); 
}

function admin_panel($name, $action, $title, $description, $info='', $localization='') {
  // create a form for admin panel
    echo '<div class="wrap">
    		<h2>'.$title.'</h2>
    		<h5>'.$info.'</h5>
    		<p>'.$description.'</p>
			<p><form name="'. $name.'" action="'.$action.'" method="post" id="'.$name.'">
			<fieldset>
    			<div class="UserOption">
   					<input type="hidden" name="page" value="'.$name.'" />';
}

function admin_field($id, $type, $text, $default, $localization='', $message='' ) {
  // add a field in form for admin panel
  //   type is the field type :
  //      littlenumber
  //      text
  //      color
  //      page : a page-break in the admin panel
  //      longtext
  //	  checkbox
  //	  hidden
  //	  button
  //	  readonly
	
	
    @list($text,$text2) = @explode('|',$text,2);
    echo '<div class="field_wrapper">';
    switch($type) {
        case 'color':
        	echo '
          <label for="'.$id.'" class="label">'.__($text,$localization).'</label>  :
          #<input type="text" id="'.$id.'" maxlength="6" name="'.$id.'" value="'.$default.'"
	      size="6" onchange="ChangeColor(\''.$id.'_box\',this.value)" onkeyup="ChangeColor(\''.$id.'_box\',this.value)"/>
	      <span id="'.$id.'_box" style="background:#'.$default.'">&nbsp;&nbsp;&nbsp;</span>
	      <div class="field_caption">'.__($message,$localization).'</div>';
        break;
        case 'page':
		echo '
             	</div>
            	</fieldset>
	            <br />
                <fieldset>
	            <legend><b>'.__($text,$localization).'</b></legend>
	            <div class="UserOption">
                <p><i>'.__($message,$localization).'</i></p>';
        break;
        case 'select':
		echo '
		<label for="'.$id.'" class="label">'.__($text,$localization).'</label>  :
          <select class="field_select" id="'.$id.'" name="'.$id.'" >';
          	$options = explode(",",$text2);
          	foreach($options as $opt) {
				$v=$opt;$k=$opt;
				
          			if (strpos($opt,':')) list($k,$v)=explode(':',$opt,2);
				if ($k=='') $k=$v;
				
				if ($default==$k)	$d = ' selected="selected" ';
          			else			$d = ' ';
				
          			echo "<option value='$k' $d >".__($v,$localization)."</option>";
          	}
          echo '
          </select>
          <div class="field_caption">'.__($message,$localization).'</div>';
        break;
        case 'littlenumber':
	case 'smallesttext':
	  echo '
          <label class="label" for="'.$id.'">'.__($text,$localization).'</label>  :
          <input id="'.$id.'" type="text" maxlength="5" name="'.$id.'" value="'.$default.'" size="3" /> '.__($text2,$localization).'<br />
	      <div class="field_caption">'.__($message,$localization).'</div>';
        break;
        case 'smalltext':
	  echo '<label class="label" for="'.$id.'">'.__($text,$localization).'</label>  :
          <input type="text" maxlength="100" name="'.$id.'" id="'.$id.'" value="'.$default.'" size="20" /> '.__($text2,$localization).'<br />
	  <div class="field_caption">'.__($message,$localization).'</div>';
        break;
        case 'text':
          echo '
          <label class="label" for="'.$id.'">'.__($text,$localization).'</label>  :
          <input type="text" maxlength="300" name="'.$id.'" id="'.$id.'" value="'.$default.'" size="60" />'.__($text2,$localization).'<br />
	  <div class="field_caption">'.__($message,$localization).'</div>';
        break;
        case 'hidden':
          echo '<input type="hidden" name="'.$id.'" id="'.$id.'" value="'.$default.'" />';
        break;
        case 'button':
        	// @TODO button non funziona
         echo '<input type="button" name="'.$id.'" id="'.$id.'" value="'.$text.'" />';
        break;
        case 'longtext':
		if ($text!='') 
			echo '<label for="'.$id.'" class="label">'.__($text,$localization).'</label>  :<br />';
		echo '<textarea  name="'.$id.'" id="'.$id.'" cols="60" rows="5" style="width:99%">'.$default.'</textarea> '.__($text2,$localization).'<br />
			<div class="field_caption">'.__($message,$localization).'</div>';
        break;
       case 'readonly':
          echo '
          <label class="label" for="'.$id.'">'.__($text,$localization).'</label>  :
	  <input type="hidden" name="'.$id.'" id="'.$id.'" value="'.$default.'" />
          <input type="text" disable="disabled" name="'.$id.'_view" id="'.$id.'_view" value="'.$default.'" size="60"  />'.__($text2,$localization).'<br />
	  <div class="field_caption">'.__($message,$localization).'</div>';
        break;
        case 'checkbox':
		echo '<label for="'.$id.'" class="label">'.__($text,$localization).'</label>  :
				<input type="checkbox" value="1" name="'.$id.'" id="'.$id.'" ';
		if($default == '1') { echo ' checked="checked" '; }
		echo ' /><br /><div class="field_caption">'.__($message,$localization).'</div>';
        break;
        
      case 'textarea':
    	echo '
		<script type="text/javascript">
		jQuery(document).ready(function() {
			jQuery("#'.$id.'").addClass("mceEditor");
			if ( typeof( tinyMCE ) == "object" && typeof( tinyMCE.execCommand ) == "function" ) {
   				tinyMCE.execCommand("mceAddControl", false, "'.$id.'");
   			} 
  		});
   		</script>';
 
   		echo '
           <label class="label" for="'.$id.'">'.__($text,$localization).'</label>  :
         <textarea class="'.$id.'" name="'.$id.'" id="'.$id.'" rows="5" cols="40"> '.$default.'</textarea>'.__($text2,$localization).'<br />
        <div class="field_caption">'.__($message,$localization).'</div>';
        break;
        
        
        
        
    }
	echo '</div>';
}

function admin_field_save($name, $val=null) {
// save option named $name, if $val is defined
	$old = get_option($name);
	if ($val!=null) {
		if (get_option($name,'')!='') {
			update_option($name,$val);
		} else {
			add_option($name,$val,'',true);
		}
	}
}

function admin_field_save_post_meta($id,$name,$value) {
// save post meta named $name. Create new if don't exist, overwrite if exist
  if (!get_post_meta($id,$name)) 
        add_post_meta($id,$name,$value,true);
  else 
        update_post_meta($id,$name,$value);
}

function admin_field_save_post_meta_multiple($id,$name,$value) {
// save post meta named $name. Create new if don't exist, add new if exist
        add_post_meta($id,$name,$value,true);
}


function admin_table($columns_name) {
// create a table with specified columns
	echo '<table class="wp-list-table widefat fixed users" cellspacing="0">
		<thead>
			<tr>';
	foreach($columns_name as $col) echo '	<th>'.$col.'</th>';
	echo '		</thead>
		<tbody>';

}

function admin_table_row($row) {
// add row to table
	echo '<tr>';
	foreach($row as $td) echo '	<td>'.$td.'</td>';
	echo '</tr>';
}

function admin_table_close($columns_name) {
// add footer to table and close it
	echo '			</tbody>
				<tfoot>';
	foreach($columns_name as $col) echo '	<th>'.$col.'</th>';
	echo '</tfoot>
	     </table>';
}

function admin_panel_close() {
  // close a form of admin panel
  echo '
    </fieldset>
    '.submit_button().'	
    </form>
    </p>
    <script>
	function ChangeColor(id,color) {
		jQuery(id).css("background-color","#"+color);
	}
	</script>
</div>
  ';
}

function admin_widget($slug,$title,$widget_form,$widget_save) {
 // create a admin widget
 
 // alias
 wp_add_dashboard_widget($slug, $title, $widget_form,$widget_save);	

 // hook with add_action('wp_dashboard_setup', 'function_name' );

	
}

admin_css();

